(*
 *
 *  This file is part of Aws-OCaml
 *
 *  Aws-OCaml is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later 
 *  
 *  Aws-OCaml is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Aws-OCaml. If not, see <http://www.gnu.org/licenses/>.
 *
 *  authentication.ml william le ferrand (william@themipsfactory.com) 26/03/2010 09:26
 *
 *)

open Misc  
open Types
 
let sign sec2 request = 
  let sorted_attributes = List.sort (fun (k1, v1)  (k2, v2) -> String.compare k1 k2) request in
  let encoded_attributes = List.map (fun (k, v) -> encode k, encode v) sorted_attributes in
  let raw_request = List.fold_left (fun acc (k,v) -> 
				      if acc = "" then 
					Printf.sprintf "%s=%s" k v 
				      else 
					Printf.sprintf "%s&%s=%s" acc k v) "" encoded_attributes in

  let string_to_sign = Printf.sprintf "%s\n%s\n%s\n%s" sec2.http_method sec2.http_host sec2.http_uri raw_request in
  let hasher = Cryptokit.MAC.hmac_sha1 sec2.secret_access_key in
  let hashed = Cryptokit.hash_string hasher string_to_sign in
  let encoder = Cryptokit.Base64.encode_multiline () in 
    Cryptokit.transform_string encoder hashed
      
	    
  
 



